package org.jeecg.modules.degression.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.degression.mapper.*;
import org.jeecg.modules.degression.pojo.*;
import org.jeecg.modules.degression.service.IDegressionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 成本管理Service
 */
@Service
public class DegressionServiceImpl implements IDegressionService {

    @Autowired
    MlccPriceSystemPingleiMapper priceSystemPingleiMapper;

    @Autowired
    MyMlccCategoryInfoMapper2 categoryInfoMapper2;

    @Autowired
    MyMlccPriceSystemCostRecordMapper priceSystemCostRecordMapper;

    @Autowired
    MyMlccDataTypeMapper dataTypeMapper;

    @Autowired
    MyFanxiuInfo3Mapper fanxiuInfoMapper;

    @Autowired
    ConstColorMapper constColorMapper;

    @Autowired
    MyMlccEmbdtypePrice2Mapper embdtypePrice2Mapper;

    @Autowired
    MlccCategoryLocaltion2Mapper localtion2Mapper;

    @Autowired
    MyMlccAddPriceInfoMapper addPriceInfoMapper;

    /**
     * 查询成本项列表
     *
     * @param page         分页器
     * @param category     类别
     * @param businessType 业务leixing
     */
    @Override
    public IPage<MlccPriceSystemPinglei> listPrice(Page<MlccPriceSystemPinglei> page, Integer category, Integer businessType) {
        QueryWrapper<MlccPriceSystemPinglei> wrapper = new QueryWrapper<>();
        if (category != null) {
            wrapper.eq("category_id", category);
        }
        if (businessType != null) {
            wrapper.eq("business_type", businessType);
        }
        wrapper.orderByDesc("create_time");
        IPage<MlccPriceSystemPinglei> price = priceSystemPingleiMapper.listPrice(page, category, businessType);
        for (MlccPriceSystemPinglei record : price.getRecords()) {
            QueryWrapper<MlccPriceSystemCostRecord> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("price_system_id", record.getId());
            Integer count = priceSystemCostRecordMapper.selectCount(wrapper1);
            if (count > 0) {
                record.setStatus(2);
            } else {
                record.setStatus(0);
            }
        }
//        Collections.reverse(price.getRecords());
        return price;
    }

    /**
     * 删除不需要的成本项
     *
     * @param id 成本id
     */
    @Override
    public int delPriceC(String id) {
        return priceSystemPingleiMapper.deleteById(id);
    }

    /**
     * 查询定制项下的小模块与模块下的类别列表
     *
     * @param categoryid 类别ID
     * @param code       小模块编码
     */
    @Override
    public MlccModuleSmallTemplate selectSmallMotule(Integer categoryid, String code) {
        MlccModuleSmallTemplate moduleSmallTemplate = priceSystemPingleiMapper.selectSmallMotule(categoryid, code);
        QueryWrapper<MlccCategoryInfo> wrapper = new QueryWrapper<>();
        if (moduleSmallTemplate != null) {
            wrapper.eq("parent_id", moduleSmallTemplate.getId());
            moduleSmallTemplate.setCategoryInfos(categoryInfoMapper2.selectList(wrapper));
        }
        return moduleSmallTemplate;
    }

    /**
     * 查询定制项下的小模块 (下拉模式)
     *
     * @param categoryid 类别ID
     */
    @Override
    public List<MlccModuleSmallTemplate> selectSmallMotule2(Integer categoryid) {
        List<MlccModuleSmallTemplate> moduleSmallTemplate = priceSystemPingleiMapper.selectSmallMotule2(categoryid);
        if (moduleSmallTemplate != null && moduleSmallTemplate.size() > 0) {
            for (MlccModuleSmallTemplate template : moduleSmallTemplate) {
                QueryWrapper<MlccCategoryInfo> wrapper = new QueryWrapper<>();
                wrapper.eq("parent_id", template.getId()).eq("topid", categoryid);
                template.setCategoryInfos(categoryInfoMapper2.selectList(wrapper));
            }
        }
        return moduleSmallTemplate;
    }

    /**
     * 获取定制项详情
     *
     * @param id 类别ID
     */
    @Override
    public MlccCategoryInfo categoryDetail(Integer id) {
        return categoryInfoMapper2.selectById(id);
    }

    /**
     * 保存定制成本项
     *
     * @param systemID     上级主键ID
     * @param systemCateID 成本管理的类别ID
     * @param categoryID   类别ID
     * @param categoryName 类别名称
     * @param price        价格
     */
    @Override
    public int savePricesavePrice(String systemID, String systemCateID, String categoryID, String categoryName, String price, String costCode) {
        Double aDouble = Double.valueOf(price);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 1、首先更新类别表的数据
        MlccCategoryInfo categoryInfo = categoryInfoMapper2.selectById(categoryID);
        if (categoryInfo != null) {
            categoryInfo.setIsSet(1).setUpdateBy(user.getRealname());
            categoryInfoMapper2.updateById(categoryInfo);
        }
        // 插入数据
        QueryWrapper<MlccPriceSystemCostRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("price_system_id", systemID).eq("cost_name", categoryName);
        MlccPriceSystemCostRecord one = priceSystemCostRecordMapper.selectOne(wrapper);
        if (one == null) {
            MlccPriceSystemCostRecord record = new MlccPriceSystemCostRecord();
            record.setCostItem(systemCateID).setCostName(categoryName).setPrice(BigDecimal.valueOf(aDouble)).setType(1)
                    .setCostId(categoryID).setCostCode(costCode)
                    .setStatus(1).setPriceSystemId(systemID).setCreateBy(user.getRealname()).setUpdateBy(user.getRealname());
            return priceSystemCostRecordMapper.insert(record);
        } else {
            return -1;
        }
    }

    /**
     * 获取保存的返修项
     *
     * @param page     分页器
     * @param systemID 上级ID
     */
    @Override
    public IPage<MlccPriceSystemCostRecord> listChengben(Page<MlccPriceSystemCostRecord> page, String systemID, String typeID) {
        QueryWrapper<MlccPriceSystemCostRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("price_system_id", systemID);
        if ("2".equals(typeID)) {
            wrapper.in("type", 2, 4, 5);
        } else {
            wrapper.eq("type", typeID);
        }
        wrapper.orderByDesc("create_time");
        IPage<MlccPriceSystemCostRecord> iPage = priceSystemCostRecordMapper.selectPage(page, wrapper);
        List<MlccPriceSystemCostRecord> records = iPage.getRecords();
        for (MlccPriceSystemCostRecord record : records) {
//            MlccCategoryInfo categoryInfo = categoryInfoMapper2.selectById(record.getCostItem());
            MlccCategoryInfo categoryInfo = priceSystemPingleiMapper.getCategory(record.getCostItem());
            if (categoryInfo != null) {
                record.setCateogryName(categoryInfo.getCategoryName());
            }
            // cnm 你为什么要吃饱了撑的该参数的名字呢？
            /*QueryWrapper<MlccCategoryInfo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("category_code", record.getCostCode());*/
        }
        return iPage;
    }

    /**
     * 删除成本项
     *
     * @param id 成本项id
     */
    @Override
    public int delChengben(String id) {
        return priceSystemCostRecordMapper.deleteById(id);
    }

    /**
     * 更新成本价
     *
     * @param id    成本数据ID
     * @param price 价格
     */
    @Override
    public int updatePrice(String id, String price) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        MlccPriceSystemCostRecord record = priceSystemCostRecordMapper.selectById(id);
        record.setPrice(BigDecimal.valueOf(Double.valueOf(price))).setUpdateBy(user.getRealname());
        return priceSystemCostRecordMapper.updateById(record);
    }

    /**
     * 加急项列表
     */
    @Override
    public List<MlccAddPriceInfo> listUrgent(String category) {
        QueryWrapper<MlccAddPriceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", category).eq("is_top", 0);
        return addPriceInfoMapper.selectList(wrapper);
    }

    /**
     * 获取系统加急项详情
     *
     * @param id 数据ID
     */
    @Override
    public MlccAddPriceInfo urgentDetail(String id) {
        return addPriceInfoMapper.selectById(id);
    }

    /**
     * 保存系统加急成本项(公用)
     *
     * @param costID       关联的参数的数据ID，如绣字的ID，绣字高度的id
     * @param typeID       类型：0定制项，1定制项下的参数，2绣字工艺，3撞色，4绣字部位，5绣字高度，6返修参数，7加急项
     * @param systemID     上级主键ID
     * @param systemCateID 成本管理的类别ID
     * @param categoryName 加急项名称
     * @param price        价格
     */
    @Override
    public int saveurgent(String costID, Integer typeID, String systemID, String systemCateID, String categoryName, String price, String costCode) {
        Double aDouble = Double.valueOf(price);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 插入数据
        QueryWrapper<MlccPriceSystemCostRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("price_system_id", systemID).eq("cost_name", categoryName).eq("cost_id", costID);
        MlccPriceSystemCostRecord one = priceSystemCostRecordMapper.selectOne(wrapper);
        // 判断是否可能重复添加
        if (one == null) {
            MlccPriceSystemCostRecord record = new MlccPriceSystemCostRecord();
            record.setCostItem(systemCateID).setCostName(categoryName).setPrice(BigDecimal.valueOf(aDouble)).setType(typeID)
                    .setCostCode(costCode)
                    .setStatus(1).setPriceSystemId(systemID).setCreateBy(user.getRealname()).setUpdateBy(user.getRealname()).setCostId(costID);
            return priceSystemCostRecordMapper.insert(record);
        } else {
            return -1;
        }
    }

    /**
     * 获取所有返修项
     *
     * @param categoryID 类别ID
     */
    @Override
    public List<MlccCategoryFanxiuInfo> listFanxiuSize(String categoryID) {
        QueryWrapper<MlccCategoryFanxiuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryID);
        return fanxiuInfoMapper.selectList(wrapper);
    }

    /**
     * 获取返修参数详情
     *
     * @param id 单休参数数据主键ID
     */
    @Override
    public MlccCategoryFanxiuInfo fanxiuDetail(String id) {
        return fanxiuInfoMapper.selectById(id);
    }

    /**
     * 查询撞色工艺
     *
     * @param code       撞色编码
     * @param categoryID 类别ID
     */
    @Override
    public MlccContrastcolorInfo contColor(String code, String categoryID) {
        QueryWrapper<MlccContrastcolorInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code).eq("top_id", categoryID);
        return constColorMapper.selectOne(wrapper);
    }

    /**
     * 查询撞色工艺 （下拉模式）
     *
     * @param categoryID 类别ID
     */
    @Override
    public List<MlccContrastcolorInfo> contColor2(String categoryID) {
        QueryWrapper<MlccContrastcolorInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("top_id", categoryID);
        return constColorMapper.selectList(wrapper);
    }

    /**
     * 查询绣字高度
     *
     * @param categoryID 类别ID
     */
    @Override
    public List<MlccEmbdtypePrice> listXiuziHeight(String categoryID) {
        QueryWrapper<MlccEmbdtypePrice> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryID);
        return embdtypePrice2Mapper.selectList(wrapper);
    }

    /**
     * 获取休息详情
     *
     * @param id 绣字高度ID
     */
    @Override
    public MlccEmbdtypePrice xiuxiDetail(String id) {
        return embdtypePrice2Mapper.selectById(id);
    }

    /**
     * 查询绣字位置集合
     *
     * @param categoryID 类别ID
     */
    @Override
    public List<MlccCategoryLocaltion> listXiuziHandler(String categoryID, String code) {
        QueryWrapper<MlccCategoryLocaltion> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryID);
        if (code != null && !"".equals(code)) {
            wrapper.eq("code", code);
        }
        return localtion2Mapper.selectList(wrapper);
    }

    /**
     * 绣字位置详情
     *
     * @param id 绣字位置ID
     */
    @Override
    public MlccCategoryLocaltion locationDetail(String id) {
        return localtion2Mapper.selectById(id);
    }
}
